﻿package game 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import game.events.EventData;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class SoundList extends MovieClip {	
		public var listBg:MovieClip;
		public var volumeSound:VolumeController;
		
		public var listSongs:XMLList;
		public var numberSongs:Number;
		
		public var xmlLoader:URLLoader = new URLLoader();
		
		public var musicList:Array;		
		
		public var currentActiveMusic = new MusicTitle();
		
		public var txtFormat:TextFormat = new TextFormat();		
		
		public function SoundList() {	
				
			this.musicList = new Array();
			xmlLoader.load(new URLRequest("playlist.xml"));
			xmlLoader.addEventListener(Event.COMPLETE, processXML);		
			volumeSound.addEventListener(EventData.UPDATE_VOLUMEBACKGROUND, hdlUpdateSoundVolumeBg);
		}
		
		private function hdlUpdateSoundVolumeBg(e:EventData):void 
		{
			var event:EventData = new EventData(EventData.UPDATE_SOUNDVOLUMEBACKGROUND, true);
			event.newBalance = e.newBalance;
			dispatchEvent(event);
			//trace("soundList:" + e.newBalance);
		}
		
		private function processXML(e:Event):void {
			var xml:XML = new XML(e.target.data);
			this.listSongs = xml.music;
			this.numberSongs = this.listSongs.length();	
			
			listMusicSong();
			
			xmlLoader.removeEventListener(Event.COMPLETE, processXML);
			xmlLoader = null;
		}
		
		private function listMusicSong() {		
			for (var i = 0; i < this.numberSongs; i++) {			
				this.musicList.push(this.listSongs[i].@url);
			}			
			addListMusicTitle();
		}
		
		private function addListMusicTitle() {			
			
			
			for (var i = 0; i < this.numberSongs; i++) {
				var musicTitle = new MusicTitle();				
				musicTitle.x = 50;
				musicTitle.y = i * 25 + 10;	
				musicTitle.name = i;
				musicTitle.stt_txt.text = "0" + (i + 1);
				if(GlobalVariables.lang == "E") {
					musicTitle.title_txt.text = this.listSongs[i].@titleE;
				} else if (GlobalVariables.lang == "G") {
					musicTitle.title_txt.text = this.listSongs[i].@titleG;
				} else if (GlobalVariables.lang == "K") {
					musicTitle.title_txt.text = this.listSongs[i].@titleK;
				} else if (GlobalVariables.lang == "B") {
					musicTitle.title_txt.text = this.listSongs[i].@titleB;
				} else if (GlobalVariables.lang == "T") {
					musicTitle.title_txt.text = this.listSongs[i].@titleT;
				} else if (GlobalVariables.lang == "J") {
					musicTitle.title_txt.text = this.listSongs[i].@titleJ;
				} else if (GlobalVariables.lang == "V") {
					musicTitle.title_txt.text = this.listSongs[i].@titleV;
				} else if (GlobalVariables.lang == "M") {
					musicTitle.title_txt.text = this.listSongs[i].@titleM;
				} else {
					musicTitle.title_txt.text = this.listSongs[i].@titleE;
				}
				
				musicTitle.title_txt.selectable = false;
								
				if (i == 0) {
					currentActiveMusic = musicTitle;		
					GlobalVariables.soundActive = this.musicList[i];
					/*var event:EventData = new EventData(EventData.UPDATE_SOUNDBACKGROUND, true);
					dispatchEvent(event);*/
				}
				
				musicTitle.useHandCursor = true;
				musicTitle.buttonMode = true;
				musicTitle.title_txt.mouseEnabled = false;
				listBg.useHandCursor = true;
				musicTitle.addEventListener(MouseEvent.ROLL_OVER, hdlListTestOver);
				musicTitle.addEventListener(MouseEvent.ROLL_OUT, hdlListTestOut);
				musicTitle.addEventListener(MouseEvent.CLICK, hdlListTestClick);
				addChild(musicTitle);				
			}
			currentActiveMusic.gotoAndStop("down");	
		}		
		
		private function hdlListTestOver(e:MouseEvent):void {			
			var musicTitle = new MusicTitle();
			musicTitle = e.currentTarget;			
			
			txtFormat.italic = true;			
			musicTitle.title_txt.setTextFormat(txtFormat);					
		}	
		
		private function hdlListTestOut(e:MouseEvent):void {			
			var musicTitle = new MusicTitle();
			musicTitle = e.currentTarget;			

			txtFormat.italic = false;			
			musicTitle.title_txt.setTextFormat(txtFormat);
		}
		
		private function hdlListTestClick(e:MouseEvent):void {
			if(currentActiveMusic.name != e.currentTarget.name) {
				currentActiveMusic.gotoAndStop("play");
				
				var musicTitle = new MusicTitle();
				musicTitle = e.currentTarget;			
				
				txtFormat.italic = true;			
				musicTitle.title_txt.setTextFormat(txtFormat);
				currentActiveMusic = e.currentTarget;
				musicTitle.gotoAndStop("down");					
				
				var pos:Number = Number(e.currentTarget.name);				
				GlobalVariables.soundActive = this.musicList[pos];
				var event:EventData = new EventData(EventData.UPDATE_SOUNDBACKGROUND, true);
				dispatchEvent(event);
			}
			
		}		
	}
}